package com.lhy.framework.beans.factory.support;

import com.lhy.framework.BeansException;
import com.lhy.framework.beans.BeanFactory;
import com.lhy.framework.beans.factory.factory.BeanDefinition;

/**
 * @ClassName AbstractBeanFactory
 * @Description
 * @Author lihaiyang
 * @Date 2022/9/8 22:21
 * @Version 1.0
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String beanName) throws BeansException {
        Object bean = getSingleton(beanName);
        if (null != bean) {
            return bean;
        }
        BeanDefinition beanDefinition = getBeanDefinition(beanName);
        return createBean(beanName, beanDefinition);
    }

    /**
     * 获取beanDefinition对象
     * @param beanName
     * @return
     * @throws BeansException
     */
    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;

    /**
     * 通过beanDefinition创建bean对象
     * @param beanName
     * @param beanDefinition
     * @return
     * @throws BeansException
     */
    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition) throws BeansException;

}
